package com.LCQ.crm.workbench.service.impl;
import com.LCQ.crm.utils.DateTimeUtil;
import com.LCQ.crm.utils.SqlSessionUtil;
import com.LCQ.crm.utils.UUIDUtil;
import com.LCQ.crm.vo.PaginationVo;
import com.LCQ.crm.workbench.dao.*;
import com.LCQ.crm.workbench.domain.Contacts;
import com.LCQ.crm.workbench.domain.Customer;
import com.LCQ.crm.workbench.domain.Tran;
import com.LCQ.crm.workbench.domain.TranHistory;
import com.LCQ.crm.workbench.service.TransactionService;

import java.util.List;
import java.util.Map;

public class TransactionServiceImpl implements TransactionService {
    private CustomerDao customerDao = SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);
    private ContactsDao contactsDao = SqlSessionUtil.getSqlSession().getMapper(ContactsDao.class);
    private ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    private TranDao tranDao = SqlSessionUtil.getSqlSession().getMapper(TranDao.class);
    private TranHistoryDao tranHistoryDao = SqlSessionUtil.getSqlSession().getMapper(TranHistoryDao.class);
    @Override
    public String[] getCustomerName(String name) {
        return customerDao.getCustomerName(name);
    }

    @Override
    public List<Contacts> getContactsByCond(Map<String, String> map) {
        return contactsDao.getContactsByCond(map);
    }

    @Override
    public List<Contacts> getActivityByCond(Map<String, String> map) {
        return activityDao.getActivityByCond(map);
    }

    @Override
    public boolean save(Tran tran, String customerName) {
        Customer customer = customerDao.getByName(customerName);
        if(customer==null){
            customer = new Customer();
            customer.setCreateBy(tran.getCreateBy());
            customer.setOwner(tran.getOwner());
            customer.setNextContactTime(tran.getNextContactTime());
            customer.setName(customerName);
            customer.setCreateTime(DateTimeUtil.getSysTime());
            customer.setContactSummary(tran.getContactSummary());
            customer.setDescription(tran.getDescription());
            customer.setId(UUIDUtil.getUUID());
            int count1 = customerDao.insert(customer);
            if(count1!=1){
                return false;
            }
        }
        tran.setCustomerId(customer.getId());
        int count2 = tranDao.insert(tran);
        if(count2!=1){
            return false;
        }
        TranHistory tranHistory = new TranHistory();
        tranHistory.setTranId(tran.getId());
        tranHistory.setStage(tran.getStage());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setId(UUIDUtil.getUUID());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setCreateTime(DateTimeUtil.getSysTime());
        tranHistory.setCreateBy(tran.getCreateBy());
        int count3 = tranHistoryDao.insert(tranHistory);
        if(count3!=1){
            return false;
        }
        return true;
    }

    @Override
    public PaginationVo<Tran> getPageList(Map<String, Object> map) {
        PaginationVo<Tran> page = new PaginationVo<>();
        page.setPageList(tranDao.getPageListByCond(map));
        page.setTotal(tranDao.getCountByCond(map));
        return page;
    }

    @Override
    public Tran getDetail(String id) {
        return tranDao.getDetailById(id);
    }

    @Override
    public List<TranHistory> getTranHistoryList(String tranId) {
        return tranHistoryDao.getListByTranId(tranId);
    }

    @Override
    public boolean changeStage(String stage, String tranId, String editBy) {
        Tran tran = tranDao.getById(tranId);
        tran.setStage(stage);
        tran.setEditBy(editBy);
        tran.setEditTime(DateTimeUtil.getSysTime());
        int count1 = tranDao.update(tran);
        if(count1!=1){
            return false;
        }

        TranHistory tranHistory = new TranHistory();
        tranHistory.setCreateBy(editBy);
        tranHistory.setCreateTime(DateTimeUtil.getSysTime());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setId(UUIDUtil.getUUID());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setStage(tran.getStage());
        tranHistory.setTranId(tran.getId());
        int count2 = tranHistoryDao.insert(tranHistory);
        if(count2!=1){
            return false;
        }
        return true;
    }
}
